package jp.co.cos.ittrick.channel.controller.client;

import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.service.PageService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class ChangePageIndexController extends Controller {

    private PageService pageService;

    public ChangePageIndexController() {
        pageService = new PageService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String page = asString("page");
        String index = asString("index");

        // Modelの設定
        PageModel model = pageService.getPageByKey(Integer.valueOf(page));
        if(model != null) {
            model.setIndex(Integer.valueOf(index));

            // Insert
            pageService.update(model);
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.getWriter().println("0");
        response.flushBuffer();

        return null;
    }
}
